package com.fjnu.controller.shiro;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.User;
import com.fjnu.service.IUserService;
/**
 * 用户界面的更删改查登录
 * @author 默默
 *
 */
@Controller

public class UserController extends BaseController{
	private static Logger logger =LogManager.getLogger(UserController.class);
	@Autowired
	private IUserService userService;
	@RequiresRoles("系统管理员")
	@ResponseBody
	@RequestMapping("/findUser")
	public Map<String,Object> findUser(
	@RequestParam(value="pageNo",required=false) Integer pageNo,
			@RequestParam(value="pageSize",required=false) Integer pageSize){
		logger.info("查询所有用户");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		if(pageSize==null){
			pageSize=10;
		}
		if(pageNo==null){
			pageNo=1;
		}
		List<User> list = userService.findUser(pageNo, pageSize);
		put("data",list);
		return getData2Result();
	}
	@RequestMapping("/updateUser")
	@ResponseBody
	public Map<String,Object> updateUser(Integer id,String name,String password){
		logger.info("更新用户");
		if(!init(id,name,password).get(STATUS).equals(SUCCESS))
			return getData2Result();
		userService.UpdateUser(id, name, password);
		return getData2Result();
	}
	@RequestMapping("/delUser")
	@ResponseBody
	public Map<String,Object> delUser( Integer id){
		logger.info("删除用户");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		userService.delUser(id);
		return getData2Result();
	}
	@RequestMapping("/addUser")
	@ResponseBody
	public Map<String,Object> addUser(@RequestBody User user){
		logger.info("添加用户");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		userService.saveUser(user);
		return getData2Result();
	}

}
